/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.controller;

import vip.dexian.admin.base.BaseController;
import vip.dexian.admin.dto.LoginLogDTO;
import vip.dexian.admin.enums.OperateTypeEnum;
import vip.dexian.admin.log.AdminLog;
import vip.dexian.admin.searcher.LoginLogPageSearcher;
import vip.dexian.admin.service.LoginLogService;
import vip.dexian.common.Message;
import vip.dexian.common.query.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录日志管理
 *
 * @author 挺好的 2023年06月07日 10:48:02
 */
@Controller ("adminLoginLogController")
@RequestMapping ("/admin/login_log")
public class LoginLogController extends BaseController {

    /**
     * 登录日志Service
     */
    @Resource (name = "loginLogServiceImpl")
    private LoginLogService loginLogService;

    /**
     * 登录日志列表页面
     *
     * @param searcher
     *         登录日志查询器
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     *
     * @return list view
     */
    @GetMapping ("/list")
    @AdminLog (module = "loginLog", description = "查看登录日志列表", type = OperateTypeEnum.LIST)
    public String page (LoginLogPageSearcher searcher, ModelMap model) {

        // 将searcher添加到model中
        searcher.addAttribute(model);

        Page <LoginLogDTO> page = this.loginLogService.findPage(searcher.query());

        model.addAttribute("page", page);

        return "/admin/admin/login_log/list";
    }

    /**
     * 进入编辑页面
     *
     * @param model
     *         {@link org.springframework.ui.ModelMap}
     * @param id
     *         登录日志id
     *
     * @return update view
     */
    @GetMapping ("/update")
    public String update (Long id, ModelMap model) {
        LoginLogDTO loginLogDTO = this.loginLogService.find(id);

        model.addAttribute("content", loginLogDTO);
        return "/admin/admin/login_log/update";
    }

    /**
     * 删除登录日志
     *
     * @param ids
     *         id列表，客户端传递的参数为： ids=1&ids=2&ids=3
     *
     * @return {@link vip.dexian.common.Message}
     */
    @PostMapping ("/delete")
    @ResponseBody
    @AdminLog (module = "loginLog", description = "删除登录日志", type = OperateTypeEnum.DELETE)
    public Message delete (Long[] ids) {
        this.loginLogService.delete(ids);
        return Message.success();
    }

}
